/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * GraphCreatorView.java
 *
 * Created on 2011-03-31, 21:40:12
 */

package cegg;

import cegg.graph.Graph;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import org.jdesktop.application.Action;

/**
 *
 * @author kumper
 */
public class GraphCreatorView extends javax.swing.JDialog {

    /** Creates new form GraphCreatorView */
    public GraphCreatorView(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    public GraphCreatorView(java.awt.Frame parent) {
        super(parent);
        initComponents();
        graph = new Graph();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        navigationBar = new javax.swing.JPanel();
        vertexMenuLabel = new javax.swing.JLabel();
        commandsMenuSeparator1 = new javax.swing.JSeparator();
        createReasonButton = new javax.swing.JButton();
        createEffectButton = new javax.swing.JButton();
        connectButton = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        createIntermediateButton = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JSeparator();
        jSeparator2 = new javax.swing.JSeparator();
        connectionMenuLabel = new javax.swing.JLabel();
        refreshViewButton = new javax.swing.JButton();
        jSeparator3 = new javax.swing.JSeparator();
        dependencyLabel = new javax.swing.JLabel();
        jSeparator4 = new javax.swing.JSeparator();
        restrictionLabel = new javax.swing.JLabel();
        restrictionButton = new javax.swing.JButton();
        saveButton = new javax.swing.JButton();
        content = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setName("Form"); // NOI18N
        setResizable(false);

        navigationBar.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        navigationBar.setName("navigationBar"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(cegg.CeggApp.class).getContext().getResourceMap(GraphCreatorView.class);
        vertexMenuLabel.setText(resourceMap.getString("vertexMenuLabel.text")); // NOI18N
        vertexMenuLabel.setName("vertexMenuLabel"); // NOI18N

        commandsMenuSeparator1.setName("commandsMenuSeparator1"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(cegg.CeggApp.class).getContext().getActionMap(GraphCreatorView.class, this);
        createReasonButton.setAction(actionMap.get("createReason")); // NOI18N
        createReasonButton.setText(resourceMap.getString("createReasonButton.text")); // NOI18N
        createReasonButton.setName("createReasonButton"); // NOI18N

        createEffectButton.setAction(actionMap.get("createEffect")); // NOI18N
        createEffectButton.setText(resourceMap.getString("createEffectButton.text")); // NOI18N
        createEffectButton.setName("createEffectButton"); // NOI18N

        connectButton.setAction(actionMap.get("createConnection")); // NOI18N
        connectButton.setText(resourceMap.getString("connectButton.text")); // NOI18N
        connectButton.setName("connectButton"); // NOI18N

        jButton4.setAction(actionMap.get("createDependency")); // NOI18N
        jButton4.setText(resourceMap.getString("jButton4.text")); // NOI18N
        jButton4.setName("jButton4"); // NOI18N

        createIntermediateButton.setAction(actionMap.get("createIntermediate")); // NOI18N
        createIntermediateButton.setText(resourceMap.getString("createIntermediateButton.text")); // NOI18N
        createIntermediateButton.setName("createIntermediateButton"); // NOI18N

        jSeparator1.setName("jSeparator1"); // NOI18N

        jSeparator2.setName("jSeparator2"); // NOI18N

        connectionMenuLabel.setText(resourceMap.getString("connectionMenuLabel.text")); // NOI18N
        connectionMenuLabel.setName("connectionMenuLabel"); // NOI18N

        refreshViewButton.setAction(actionMap.get("refreshViewButton")); // NOI18N
        refreshViewButton.setText(resourceMap.getString("refreshViewButton.text")); // NOI18N
        refreshViewButton.setName("refreshViewButton"); // NOI18N

        jSeparator3.setName("jSeparator3"); // NOI18N

        dependencyLabel.setText(resourceMap.getString("dependencyLabel.text")); // NOI18N
        dependencyLabel.setName("dependencyLabel"); // NOI18N

        jSeparator4.setName("jSeparator4"); // NOI18N

        restrictionLabel.setText(resourceMap.getString("restrictionLabel.text")); // NOI18N
        restrictionLabel.setName("restrictionLabel"); // NOI18N

        restrictionButton.setAction(actionMap.get("createRestriction")); // NOI18N
        restrictionButton.setText(resourceMap.getString("restrictionButton.text")); // NOI18N
        restrictionButton.setName("restrictionButton"); // NOI18N

        saveButton.setAction(actionMap.get("saveGraph")); // NOI18N
        saveButton.setText(resourceMap.getString("saveButton.text")); // NOI18N
        saveButton.setName("saveButton"); // NOI18N

        javax.swing.GroupLayout navigationBarLayout = new javax.swing.GroupLayout(navigationBar);
        navigationBar.setLayout(navigationBarLayout);
        navigationBarLayout.setHorizontalGroup(
            navigationBarLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, navigationBarLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(vertexMenuLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 117, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(navigationBarLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(createReasonButton, javax.swing.GroupLayout.DEFAULT_SIZE, 117, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(navigationBarLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(createEffectButton, javax.swing.GroupLayout.DEFAULT_SIZE, 117, Short.MAX_VALUE)
                .addContainerGap())
            .addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 137, Short.MAX_VALUE)
            .addComponent(commandsMenuSeparator1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 137, Short.MAX_VALUE)
            .addComponent(jSeparator2, javax.swing.GroupLayout.DEFAULT_SIZE, 137, Short.MAX_VALUE)
            .addGroup(navigationBarLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(connectButton, javax.swing.GroupLayout.DEFAULT_SIZE, 117, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(navigationBarLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(refreshViewButton, javax.swing.GroupLayout.DEFAULT_SIZE, 117, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(navigationBarLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(connectionMenuLabel)
                .addContainerGap(69, Short.MAX_VALUE))
            .addComponent(jSeparator3, javax.swing.GroupLayout.DEFAULT_SIZE, 137, Short.MAX_VALUE)
            .addGroup(navigationBarLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(dependencyLabel)
                .addContainerGap(63, Short.MAX_VALUE))
            .addComponent(jSeparator4, javax.swing.GroupLayout.DEFAULT_SIZE, 137, Short.MAX_VALUE)
            .addGroup(navigationBarLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(restrictionLabel)
                .addContainerGap(72, Short.MAX_VALUE))
            .addGroup(navigationBarLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jButton4, javax.swing.GroupLayout.DEFAULT_SIZE, 117, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(navigationBarLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(restrictionButton, javax.swing.GroupLayout.DEFAULT_SIZE, 117, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(navigationBarLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(createIntermediateButton, javax.swing.GroupLayout.DEFAULT_SIZE, 117, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(navigationBarLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(saveButton, javax.swing.GroupLayout.DEFAULT_SIZE, 117, Short.MAX_VALUE)
                .addContainerGap())
        );
        navigationBarLayout.setVerticalGroup(
            navigationBarLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(navigationBarLayout.createSequentialGroup()
                .addComponent(vertexMenuLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(commandsMenuSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(5, 5, 5)
                .addComponent(createReasonButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(createIntermediateButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(createEffectButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(1, 1, 1)
                .addComponent(connectionMenuLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(connectButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator3, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(dependencyLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator4, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(restrictionLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(restrictionButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 183, Short.MAX_VALUE)
                .addComponent(saveButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(refreshViewButton)
                .addContainerGap())
        );

        content.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        content.setName("content"); // NOI18N

        javax.swing.GroupLayout contentLayout = new javax.swing.GroupLayout(content);
        content.setLayout(contentLayout);
        contentLayout.setHorizontalGroup(
            contentLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 647, Short.MAX_VALUE)
        );
        contentLayout.setVerticalGroup(
            contentLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 581, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(navigationBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(content, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(content, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(navigationBar, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                GraphCreatorView dialog = new GraphCreatorView(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    @Action
    public void createReason() {
        if (graph.countReasons() < 10)
        {
            int posx = 100;
            int posy = (graph.countReasons() + 1) * 50;
            int id = graph.countReasons() + 1;
            graph.createReason(id, posx, posy);
        }
        else
        {
            throw new IllegalStateException("Cannot create more than 10 reason vertexes!");
        }
        this.repaintContent();
    }

    @Action
    public void createEffect() {
        if (graph.countEffects() < 10)
        {
            int posx = content.getWidth()-100;
            int posy = (graph.countEffects() + 1) * 50;
            int id = graph.countEffects() + 51;
            graph.createEffect(id, posx, posy);
        }
        else
        {
            throw new IllegalStateException("Cannot create more than 10 effect vertexes!");
        }
        this.repaintContent();
    }

    @Action
    public void createIntermediate() {
        if (graph.countIntermediate() < 10)
        {
            int posx = content.getWidth()/2;
            int posy = (graph.countIntermediate() + 1) * 50;
            int id = graph.countIntermediate() + 71;
            graph.createIntermediate(id, posx, posy);
        }
        else
        {
            throw new IllegalStateException("Cannot create more than 10 intermediate vertexes!");
        }
        this.repaintContent();
    }

    public void repaintContent()
    {
        graph.paint(content.getGraphics());
    }

    public Graph getGraph() {
        if (graph != null)
        {
            return this.graph;
        }
        else
        {
            throw new NullPointerException("Graph is not created yet.");
        }
    }

    @Action
    public void createConnection() {
        if (connectionView == null) {
            JFrame mainFrame = CeggApp.getApplication().getMainFrame();
            connectionView = new CreateConnectionView(mainFrame, false, graph);
            connectionView.setLocationByPlatform(true);
        }
        CeggApp.getApplication().show(connectionView);
    }

    @Action
    public void refreshViewButton() {
        this.repaintContent();
    }

    @Action
    public void createDependency() {
        if (dependencyView == null) {
            JFrame mainFrame = CeggApp.getApplication().getMainFrame();
            dependencyView = new CreateDependencyView(mainFrame, false, graph);
            dependencyView.setLocationByPlatform(true);
        }
        CeggApp.getApplication().show(dependencyView);
    }

    @Action
    public void createRestriction() {
        if (restrictionView == null) {
            JFrame mainFrame = CeggApp.getApplication().getMainFrame();
            restrictionView = new CreateRestrictionView(mainFrame, false, graph);
            restrictionView.setLocationByPlatform(true);
        }
        CeggApp.getApplication().show(restrictionView);
    }

    @Action
    public void saveGraph() {
        FileOutputStream fos = null;
        ObjectOutputStream oos = null;
        try {
            fos = new FileOutputStream("C:\\temp\\graph.ser");
            oos = new ObjectOutputStream(fos);
            oos.writeObject(graph);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(GraphCreatorView.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(GraphCreatorView.class.getName()).log(Level.SEVERE, null, ex);
        }

        InformationDialog info =
            new InformationDialog(CeggApp.getApplication().getMainFrame(), true);
        info.setInfoText("Graph has been saved to: C:\\temp\\graph.ser");
        info.setVisible(true);
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JSeparator commandsMenuSeparator1;
    private javax.swing.JButton connectButton;
    private javax.swing.JLabel connectionMenuLabel;
    private javax.swing.JPanel content;
    private javax.swing.JButton createEffectButton;
    private javax.swing.JButton createIntermediateButton;
    private javax.swing.JButton createReasonButton;
    private javax.swing.JLabel dependencyLabel;
    private javax.swing.JButton jButton4;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JSeparator jSeparator3;
    private javax.swing.JSeparator jSeparator4;
    private javax.swing.JPanel navigationBar;
    private javax.swing.JButton refreshViewButton;
    private javax.swing.JButton restrictionButton;
    private javax.swing.JLabel restrictionLabel;
    private javax.swing.JButton saveButton;
    private javax.swing.JLabel vertexMenuLabel;
    // End of variables declaration//GEN-END:variables

    private cegg.graph.Graph graph;
    private cegg.CreateConnectionView connectionView;
    private cegg.CreateDependencyView dependencyView;
    private cegg.CreateRestrictionView restrictionView;
}
